#include "userMain.h"
#include "userPCF8563driver.h"

#define     PCF8563_WRITE_ADDR  0xA2
#define     PCF8563_READ_ADDR   0xA3

#define     REG_SEC     0x02
#define     REG_MIN     0x03
#define     REG_HOUR    0x04
#define     REG_DAY     0x05
#define     REG_WEEK    0x06
#define     REG_MONTH   0x07
#define     REG_YEAR    0x08

#define     SUCCESS      0x01
#define     FAIL        0x00

uint8_t userPCF8563_WriteReg(uint8_t regAddr,uint8_t regValue)
{
    DE_Ax_IIC_INITIAL;
    DE_Ax_IIC_START;
    Ax_Write8Bit(PCF8563_WRITE_ADDR);
    if(DE_Ax_TESTACK)
    {
        DE_Ax_IIC_STOP;
        DE_Ax_IIC_END;
        return FAIL;
    }
    Ax_Write8Bit(regAddr);
    if(DE_Ax_TESTACK)
    {
        DE_Ax_IIC_STOP;
        DE_Ax_IIC_END;
        return FAIL;
    }
    Ax_Write8Bit(regValue);
    if(DE_Ax_TESTACK)
    {   
        DE_Ax_IIC_STOP;
        DE_Ax_IIC_END;
        return FAIL;
    }
    DE_Ax_IIC_STOP;
    DE_Ax_IIC_END;
    return SUCCESS;
}

uint8_t userPCF8563_ReadReg(uint8_t regAddr,uint8_t *regValue)
{
    DE_Ax_IIC_INITIAL;
    DE_Ax_IIC_START;
    Ax_Write8Bit(PCF8563_WRITE_ADDR);
    if(DE_Ax_TESTACK)
    {
        DE_Ax_IIC_STOP;
        DE_Ax_IIC_END;
        return FAIL;
    }
    Ax_Write8Bit(regAddr);
    if(DE_Ax_TESTACK)
    {
        DE_Ax_IIC_STOP;
        DE_Ax_IIC_END;
        return FAIL;
    }
    DE_Ax_IIC_START;
    Ax_Write8Bit(PCF8563_READ_ADDR);
    if(DE_Ax_TESTACK)
    {
        DE_Ax_IIC_STOP;
        DE_Ax_IIC_END;
        return FAIL;
    }
    *regValue = DE_Ax_READ8BIT;
    DE_Ax_NOACK;
    DE_Ax_IIC_STOP;
    DE_Ax_IIC_END;
    return SUCCESS;
}

uint8_t userPCF8563_ReadNReg(uint8_t regAddr,uint8_t num,uint8_t *buff)
{
    DE_Ax_IIC_INITIAL;
    DE_Ax_IIC_START;
    Ax_Write8Bit(PCF8563_WRITE_ADDR);
    if(DE_Ax_TESTACK)
    {
        DE_Ax_IIC_STOP;
        DE_Ax_IIC_END;
        return FAIL;
    }
    Ax_Write8Bit(regAddr);
    if(DE_Ax_TESTACK)
    {
        DE_Ax_IIC_STOP;
        DE_Ax_IIC_END;
        return FAIL;
    }
    DE_Ax_IIC_START;
    Ax_Write8Bit(PCF8563_READ_ADDR);
    if(DE_Ax_TESTACK)
    {
        DE_Ax_IIC_STOP;
        DE_Ax_IIC_END;
        return FAIL;
    }
    uint8_t i=0;
    while(num--)
    {
        buff[i++] = DE_Ax_READ8BIT;
        DE_Ax_ACK;
    }
    DE_Ax_NOACK;
    DE_Ax_IIC_STOP;
    DE_Ax_IIC_END;
    return SUCCESS;
}

                                 // SEC   MIN   HOUR  DAY   WEEK  MON   YEAR
uint8_t PCF8563_TimeInitValue[7] = {0x00, 0x54, 0x10, 0x17, 0x01, 0x07, 0x17};
void userPCF8563_Initial(void)
{
    uint8_t user_i;
    DE_Ax_IIC_INITIAL;
    for(user_i = 8; user_i > 1;user_i--)
    {
       while(userPCF8563_WriteReg(user_i,PCF8563_TimeInitValue[user_i-2]) != SUCCESS)
            _NOP;
    }
    while( userPCF8563_WriteReg(0x00,0x00) != SUCCESS)
      _NOP;
}

uint8_t userTimeBuff[8];
void userGetTimeFromPCF8563(void)
{
    uint8_t user_i;
    while(1)
    {
      //userPCF8563_ReadNReg(0x02,0x07,userBuff);
      if(userPCF8563_ReadNReg(0x02,0x07,userTimeBuff) == SUCCESS)
      {
          for(user_i=0;user_i<7;user_i++)
          {
            if((userTimeBuff[user_i]&0x0F) >= 0x0A)
            {
               break;
            }
          }
          if(user_i > 6)
          {
              break;
          }
      }
    }
    userTimeBuff[0] &= 0x7f;
    userTimeBuff[1] &= 0x7f;
    userTimeBuff[2] &= 0x3f;
    userTimeBuff[3] &= 0x3f;
    userTimeBuff[4] &= 0x07;
    userTimeBuff[5] &= 0x1f;
    //userPCF8563_ReadReg(0x02,userSEC);
    _NOP;
}
